package org.streets.eis.component.combobox;

import org.apache.wicket.Application;
import org.apache.wicket.IRequestTarget;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.protocol.http.WebResponse;

import org.streets.eis.component.WebResources;

public class SelectorBehavior extends AbstractDefaultAjaxBehavior{

	private static final long serialVersionUID = 1L;


	public SelectorBehavior()	{
	}
	
	@Override
	protected void onBind()	{
		this.getComponent().setOutputMarkupId(true);
	}

	@Override
	protected void onComponentRendered(){
		Response response = this.getComponent().getResponse();
		
		response.write("<img style=\"");
		response.write(getIconStyle());
		response.write("\"");
		response.write(" width=\"22px\" height=\"22px\"  ");
		response.write(" id=\"");
		response.write(this.getComponent().getMarkupId()+"_$Icon");
		response.write("\" src=\"");
		response.write(this.getComponent().getRequestCycle().urlFor(WebResources.Streets.ComboBox.SELECTOR_ICON));
		response.write("\" ");
		
		if (this.getComponent().isEnabled()){
			response.write(" title=\"点击选择\" ");
			response.write(" clickUrl=\"");
			response.write(this.getCallbackUrl());
			response.write("\" ");
			if (canShowPopup()){
				response.write(" showPopup=\"1\"");
			}
			response.write(" onmouseover=\"this.src=this.src.replace('selector.gif','selector_over.gif');\" ");
			response.write(" onmouseout=\"this.src=this.src.replace('selector_over.gif','selector.gif')\" ");			
		} else {
			response.write(" disabled=\"disabled\" " );			
		}

		response.write(" alt=\"选择\" ");
		response.write(" />");
	}

	protected String getIconStyle(){
		if (getComponent().isEnabled()){
			return "position:absolute;visibility:hidden;z-Index:1;cursor: pointer;";
		} else {
			return "position:absolute;visibility:hidden;z-Index:1";
		}
	}
	
	@Override
	protected void respond(AjaxRequestTarget target){
		if (canShowPopup()) {
			IRequestTarget t = new IRequestTarget(){
				public void respond(RequestCycle requestCycle)	{
					WebResponse r = (WebResponse)requestCycle.getResponse();
		
					final String encoding = Application.get().getRequestCycleSettings().getResponseRequestEncoding();
					r.setCharacterEncoding(encoding);
					r.setContentType("text/xml; charset=" + encoding);
		
					r.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
					r.setHeader("Cache-Control", "no-cache, must-revalidate");
					r.setHeader("Pragma", "no-cache");
					
					SelectorBehavior.this.onIconClicked(r);
				}
		
				public void detach(RequestCycle requestCycle){
				}
		
			};
			final RequestCycle requestCycle = RequestCycle.get();
			requestCycle.setRequestTarget(t);
		} else{
			onIconClicked(target);
		}
	}

	protected boolean canShowPopup(){
		return true;
	}
	
	protected void onIconClicked(Response response){
	}

	protected void onIconClicked(AjaxRequestTarget target){
	}
}
